import Vapor

struct ReferenceImageController: RouteCollection {

  func boot(routes: RoutesBuilder) throws {
    let routes = routes.grouped("api", "referenceimage")
    routes.get(use: getAllHandler)
    routes.post(use: createHandler)
//    routes.get(":simplebuildingID", use: getHandler)
    routes.put(use: updateHandler)
    routes.delete(":id", use: deleteHandler)
//    routes.get("search", use: searchHandler)

  }

    func getAllHandler(_ req: Request) throws -> EventLoopFuture<[ReferenceImage]> {
        ReferenceImage.query(on: req.db).all()
    }
    
    func createHandler(_ req: Request) throws -> EventLoopFuture<ReferenceImage> {
      let data = try req.content.decode(ReferenceImageData.self)

        let image = ReferenceImage(data.id ,data.fileOcId!, data.imageShouldBeDeleted, data.isChecked, data.canBeShownInExhibitionMode, data.productGroupBreedersBroilers, data.productGroupBreedersLayers, data.productGroupBroilersColony, data.productGroupLayersAlternative, data.productGroupLayersConventional, data.productGroupLayersEnriched , data.productGroupManureComposting, data.productGroupManureDrying, data.productGroupManureTransport, data.productGroupOthersEggHandling, data.productGroupOthersFeedHandling, data.productGroupOthersOthers, data.productGroupOthersVentilation, data.productGroupPulletsAlternative, data.productGroupPulletsConventional, data.referenceID)

      return image.save(on: req.db).map { image }
    }

//    func getHandler(_ req: Request) throws -> EventLoopFuture<SimpleBuilding> {
//        SimpleBuilding.find(req.parameters.get("simplebuildingID"), on: req.db)
//      .unwrap(or: Abort(.notFound))
//    }

    func updateHandler(_ req: Request) throws -> EventLoopFuture<ReferenceImage> {
      let updateData = try req.content.decode(ReferenceImageData.self)
        return ReferenceImage.find(updateData.id, on: req.db)
        .unwrap(or: Abort(.notFound)).flatMap { referenceImage in
            referenceImage.fileOcId = updateData.fileOcId
            referenceImage.imageShouldBeDeleted = updateData.imageShouldBeDeleted
            referenceImage.isChecked = updateData.isChecked
            referenceImage.canBeShownInExhibitionMode = updateData.canBeShownInExhibitionMode
            referenceImage.productGroupBreedersBroilers = updateData.productGroupBreedersBroilers
            referenceImage.productGroupBreedersLayers = updateData.productGroupBreedersLayers
            referenceImage.productGroupBroilersColony = updateData.productGroupBroilersColony
            referenceImage.productGroupLayersAlternative = updateData.productGroupLayersAlternative
            referenceImage.productGroupLayersConventional = updateData.productGroupLayersConventional
            referenceImage.productGroupLayersEnriched = updateData.productGroupLayersEnriched
            referenceImage.productGroupManureComposting = updateData.productGroupManureComposting
            referenceImage.productGroupManureDrying = updateData.productGroupManureDrying
            referenceImage.productGroupManureTransport = updateData.productGroupManureTransport
            referenceImage.productGroupOthersEggHandling = updateData.productGroupOthersEggHandling
            referenceImage.productGroupOthersFeedHandling = updateData.productGroupOthersFeedHandling
            referenceImage.productGroupOthersOthers = updateData.productGroupOthersOthers
            referenceImage.productGroupOthersVentilation = updateData.productGroupOthersVentilation
            referenceImage.productGroupPulletsAlternative = updateData.productGroupPulletsAlternative
            referenceImage.productGroupPulletsConventional = updateData.productGroupPulletsConventional
            referenceImage.$reference.id = updateData.referenceID
          return referenceImage.save(on: req.db).map {
            referenceImage
          }
      }
    }

    func deleteHandler(_ req: Request)
      throws -> EventLoopFuture<HTTPStatus> {
        ReferenceImage.find(req.parameters.get("id"), on: req.db)
        .unwrap(or: Abort(.notFound))
        .flatMap { referenceImage in
            referenceImage.delete(on: req.db)
            .transform(to: .noContent)
      }
    }


}


struct ReferenceImageData: Content {
    var id: UUID?
    
    var fileOcId: String?
    var imageShouldBeDeleted: Bool
    var isChecked: Bool
    var canBeShownInExhibitionMode: Bool
    var productGroupBreedersBroilers: Bool
    var productGroupBreedersLayers: Bool
    var productGroupBroilersColony: Bool
    var productGroupLayersAlternative: Bool
    var productGroupLayersConventional: Bool
    var productGroupLayersEnriched: Bool
    var productGroupManureComposting: Bool
    var productGroupManureDrying: Bool
    var productGroupManureTransport: Bool
    var productGroupOthersEggHandling: Bool
    var productGroupOthersFeedHandling: Bool
    var productGroupOthersOthers: Bool
    var productGroupOthersVentilation: Bool
    var productGroupPulletsAlternative: Bool
    var productGroupPulletsConventional: Bool
    var referenceID: UUID
}
